package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;


public class TCPURLClient {

	public TCPURLClient( String url ) throws UnknownHostException, IOException {
		
			//Socket connection = new Socket("google.com.ar",80);
	    // Create socket that is connected to server on specified port
        Socket socket = new Socket(url, 80);
        System.out.println("Connected to http server...sending request");
        
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        
        // Send the encoded string to the server
        String request = "GET http://" + url + "/ HTTP/1.0\n\n\n";
        System.out.println(request);
        
        byte[] data = request.getBytes();
        
        out.write(data);
        
        // Receive the same string back from the server
        int totalBytesRcvd = 0; // Total bytes received so far
        int bytesRcvd;
        // Bytes received in last read
        while (totalBytesRcvd < data.length) {
            if ((bytesRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1) 
                throw new SocketException("Connection closed prematurely");
            totalBytesRcvd += bytesRcvd;
            //System.out.print('.');
        } 
        // data array is full
        System.out.println("Received: " + new String(data));
        // Close the socket and its streams
        socket.close();
	}
	
}
